// import _local from "../../cache/_local";
import _regular from "../../../utils/_regular";
import _object from "../../../utils/_object";
import lib_activity from "../../../api/lib_activity";
import lib_city from "../../../api/lib_city";
import _layer from "../../../common/_layer";
import _time from "../../../utils/_time"
import lib_car from "../../../api/lib_car";
import swiperData from "../../../common/_swiperdata";
import lib_clue from "../../../api/lib_clue";
import _report from "../../../common/_report"
import lib_tiktok from "../../../api/lib_tiktok";
import _city from "../../../cache/_city";
import _series from "../../../cache/_series";
import _activity from "../../../cache/_activity";


Page({
  data: {
    /**页面参数 */
    activity_id: '', //活动ID
    channel: 3, //渠道
    tids: 0, // 模板ID

    /**项目信息 */

    custom_car: 0, // 是否自定义车型库
    tid: 0, // 有无车型库
    share_theme: '', //分享描述
    stime: 0, // 项目开始时间
    etime: 0, // 项目结束时间
    ui: {
      head: '', //头图
      tail: '', //详情图
      share: '', //分享图
      bg_color: '', //表单背景色
      font_color: '', //文本颜色
      btn_bg_color: '', //按钮背景色
      btn_font_color: '', //按钮文本颜色
      btn_text: '', //按钮文本值
      btm_btn_text: '', //底部按钮文本
      is_banner: 0, //是否有轮播图
      is_counter: 0, //是否有倒计时
      is_copyright: 1, //协议
      jmp_url: 0, //0-报名成功;1-帮朋友报名;2-置换二手车;3-邮寄门票
    },

    /**form表单 */
    city: '', //城市
    brand: '', //品牌
    series: '', //车型
    name: '', // 姓名
    phone: '', //电话

    series_id: 0,

    //城市联级选择
    is_city: false, //表单城市项，默认无城市
    is_stop: false, //活动是否已结束


    /**城市相关 */

    swiper_list: [], //轮播
    clickid: '', //点击上报id

    guiding_price: 0,
    min_price: 0,
    final_price: 0,
    total_queries: 35122, //累计查询人数
    innerHeight: 827,

    is_policy: false


  },

  onLoad: function (options) {

  },
  onShow() {

    if (_activity.get() && _object._length(_activity.get()) > 0) {
      this.setData({
        activity_id: _activity.get().activity_id,
        tids: _activity.get().tids,
        clickid: _activity.get().clickid,
      })
    }

    // let cach_series = _series.get(this.data.activity_id)
    let cach_series = _series.get()
    if (cach_series && _object._length(cach_series) > 0) {
      this.setData({
        series: cach_series.series_name,
        series_id: cach_series.series_id
      })
    }

    this.setData({
      swiper_list: swiperData.time_swiper,
      city: _city.get(),
      min_price: getApp().single_globalData.msrp_min,
      final_price: getApp().single_globalData.msrp_max,
      guiding_price: getApp().single_globalData.guiding_price
    });

    // console.log(this.data.series, this.data.series_id, '切换');
    this.init_info()

  },

  init_info() {
    const { activity_id, tids } = this.data

    lib_activity.info(activity_id, tids).then((res) => {
      if (res.code != 0) {
        _layer.message(res.msg);
        return;
      }

      let data = res.data
      this.setData({
        share_theme: data.activity.share_theme,
        tid: data.activity.tid,
        custom_car: data.activity.custom_car
      })

      if (data.ui == null) {
        this.setData({ ui: {} })
        return;
      }
      this.setData({ ui: data.ui })

      // 项目结束
      if (!data.activity.state || (_time._current() > data.activity.etime)) {
        this.setData({
          is_stop: true
        })
        return;
      }
    })
  },

  //获取本机手机号
  getPhoneNumber(e) {
    // console.log(e.detail.errMsg);
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      const iv = e.detail.iv;
      const encryptedData = e.detail.encryptedData;
      lib_tiktok.phone(encryptedData, iv).then((res) => {
        if (res.code == 0) {
          this.setData({ phone: res.data.phone })

        } else {
          _layer.message(res.msg)
        }
      })
    }
  },

  //跳转
  changeSeries() {
    tt.navigateTo({
      url: `/packageMy/pages/series/series`,
    });
   
  },
  toCity() {
    tt.navigateTo({
      url: `/packageMy/pages/city/city?city=${this.data.city}`,
    });
  },
  //绑定
  ChangePhone(e) {
    this.setData({ phone: e.detail });
  },
  ChangeName(e) {
    this.setData({ name: e.detail });
  },

  // 表单提交
  submit() {

    if (!this.data.series) {
      _layer.message('请选择车型')
      return;
    }
    if (!this.data.city) {
      _layer.message('请选择城市')
      return;
    }
    // if (!this.data.name) {
    //   _layer.message('请输入姓名')
    //   return;
    // }
    // if (this.data.name && this.data.name.length > 50) {
    //   _layer.message('请输入正确的姓名')
    //   return;
    // }
    if (!this.data.phone) {
      _layer.message('请输入手机号')
      return;
    }
    if (!_regular.is_mobile(this.data.phone)) {
      _layer.message('请输入正确的手机号')
      return;
    }
    // 报名
    // let location = this.data.is_city ? (this.data.currProvince + '+' + this.data.currCity) : '';
    let form_ui_id = this.data.ui.hasOwnProperty("id") ? this.data.ui.id : 0;

    let pages = getCurrentPages(); // 获取页面指针数组
    let currentPage = pages[pages.length - 1]; // 获取当前页
    let route = currentPage.route;
    // let { activity_id, tids } = currentPage.options;
    const {activity_id, tids } = this.data
    let url = `${route}?activity_id=${activity_id}&tids=${tids}`

    let location = this.data.city

    let params = {
      activity_id: this.data.activity_id,
      channel_code: this.data.channel,
      location, // 省 + 市
      url,
      form_name: '直播购车客户',
      form_phone: this.data.phone,
      form_brand: this.data.brand,
      form_series: this.data.series,
      form_ui_id,
    }
    // console.log(params, '入参');
    // return;

    lib_clue.receive(params).then(res => {
      // console.log(res, '报名结果');
      // if (res.code != 0) {
      //   _layer.message(res.msg);
      //   return;
      // }

      // _layer.message('报名成功！')
      //监测
      if (res.code == 0) {
        _report._post(this.data.clickid)

      }
      //跳转地址
      tt.navigateTo({
        url: `/packageMy/pages/pricelist/pricelist`,
      });

      
    })
  },

  //分享
  async onShareAppMessage(option) {
    // var pages = getCurrentPages();
    // var firstPage = pages[0];
    // console.log(firstPage);

    const { activity_id, tids, clickid } = this.data

    return {
      title: '查询底价',
      path: `/pages/single/list/list?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: '/assets/image/banner.png', // 支持本地或远程图片，默认是小程序 icon
      success(res) {
        console.log('分享成功', res);
      },
      fail() {
        console.log("转发发布器调起失败");
      },
    };
  },

  policy() {
    this.setData({
      is_policy: true
    })
  },
  close_policy() {
    this.setData({
      is_policy: false
    })
  },

});